package com.rentao.custom.mybatis;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.InvocationHandler;

/**
 * <p>MapperProxy .
 *
 * <p>change history:
 *
 * <pre>
 * date         defect       person          comments
 * --------------------------------------------------------------------------------
 * 2025/6/29    ********     Tao Ren    create file.
 * </pre>
 *
 * @author Tao Ren
 * @date 2025/6/29 21:48
 */
//mapper 代理类（寻找sql）
public class MapperProxy implements InvocationHandler {

    //最后执行sql还是需要由Sqlsession来执行，注入SqlSession
    private SqlSession sqlSession;

    public MapperProxy(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //通过方法获取类路径，判断是否匹配自定义的Xml类
        if(method.getDeclaringClass().getName().equals(MyConfiguration.TestMapperXml.namespace)){
            //匹配，通过方法名获取对应的sql语句
            String sql = MyConfiguration.TestMapperXml.methodAndSql.get(method.getName());
            return sqlSession.selectOne(sql, (String) args[0]);
        }
        return method.invoke(proxy,args);
    }
}

